/**
 * 封装关于时间的工具类
 * */
class HTime {
  // 把秒数转为 00:00
  static transformSVF(value = 0,isFixed = true){
    const totalSecond = Number(value);
    const hours = Math.floor(totalSecond / (Number('60') * Number('60')));
    const minutes = Math.floor(totalSecond / Number('60')) % Number('60');
    let second;
    if(isFixed){
      second = (totalSecond % Number('60')).toFixed(0);
    }else{
      second = totalSecond % Number('60');
    }
    let hoursText = '';
    let minutesText = '';
    let secondText = '';
    if(hours < Number('10')){
      hoursText = `0${hours}`;
    }else{
      hoursText = `${hours}`;
    }
    if(minutes < Number('10')){
      minutesText = `0${minutes}`;
    }else{
      minutesText = `${minutes}`;
    }
    if(second < Number('10')){
      secondText = `0${second}`;
    }else{
      secondText = `${second}`;
    }
    if(hours !== 0){
      return `${hoursText}:${minutesText}:${secondText}`;
    }else{
      return `${minutesText}:${secondText}`;
    }
  }
  // 将日期对象按指定格式进行格式化 如 'YYYY-MM-DD hh:mm:ss'
  static dateFormat(date,format){
    const NUM1 = 3;
    const NUM2 = 4;
    const o = {
      'M+':date.getMonth() + 1, // month
      'D+':date.getDate(), // day
      'h+':date.getHours(), // hour
      'm+':date.getMinutes(), // minute
      's+':date.getSeconds(), // second
      'q+':Math.floor((date.getMonth() + NUM1) / NUM1), // quarter
      S:date.getMilliseconds(), // millisecond
    };
    if(/(Y+)/.test(format)){
      format = format.replace(RegExp.$1,date
        .getFullYear()
        .toString()
        .substr(NUM2 - RegExp.$1.length));
    }
    Object.keys(o).forEach(k => {
      if(new RegExp(`(${k})`).test(format)){
        format = format.replace(RegExp.$1,RegExp.$1.length === 1?o[k]:`00${o[k]}`.substr(o[k].toString().length));
      }
    });
    return format;
  }
}

export default HTime